package org.action.order.controller;

import org.action.order.domain.entity.ActionUserPay;
import org.action.order.domain.pojo.ActionOrder;
import org.action.order.sercive.IActionTicketUserService;
import org.action.order.sercive.IActionUserPayService;
import org.common.util.JwtUtils;
import org.common.util.Result;
import org.common.util.ThreadlocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/actionOrder")
public class ActionOrderController {
    @Autowired
    private IActionTicketUserService iActionTicketUserService;

    @Autowired
    private IActionUserPayService iActionUserPayService;
    //进行活动报名
    @PostMapping("/signUp")
    public Result signUp(@RequestBody ActionOrder actionOrder) throws InterruptedException {
        System.out.println(actionOrder);
        return iActionTicketUserService.saveActionOrder(actionOrder);
    }

    //获取用户的我的预约
    @GetMapping("/getMyOrder")
    public Result getMyOrder() {
        if(!JwtUtils.validateJwt(ThreadlocalUtils.get())) return Result.error("请登录");
        String phone= JwtUtils.getJwtUsername(ThreadlocalUtils.get());
        return Result.success(iActionUserPayService.getMyOrder(phone));
    }

    //根据订单id获取订单信息
    @GetMapping("/getOrderById/{orderId}")
    public Result getOrderById(@PathVariable("orderId") Long orderId) {
        return iActionTicketUserService.getActionOrderById(orderId);
    }

    //进行订单的支付
    @PutMapping("/payOrder")
    @Transactional
    public Result payOrder(@RequestBody Long orderId) {
        System.out.println("订单id"+orderId);
        boolean flag=iActionUserPayService.payOrder(orderId);
        if(!flag) return Result.error("支付失败");
        return Result.success("支付成功");
    }
}
